package com.innovate.utils;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Base64;

import com.gayaapi.common.util.Constant;


	/**
	 * base64封装的3des加解密
	 * @throws Exception
	 */
public class Base64DesEncrypt {

	private static Base64DesEncrypt inst = null;

	private static final String DES_KEY = "12345678;";

	private static final byte[] DES_IV = { (byte) 0x12, (byte) 0x34,
			(byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD,
			(byte) 0xEF };

	private AlgorithmParameterSpec iv = null;

	private Key key = null;

	private Cipher cipher = null;
	/**
	 * 无参构造器
	 * @throws Exception
	 */
	private Base64DesEncrypt() throws Exception {
		DESKeySpec keySpec = new DESKeySpec(DES_KEY.getBytes("UTF-8"));
		iv = new IvParameterSpec(DES_IV);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		key = keyFactory.generateSecret(keySpec);
		cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
	}
	/**
	 * 有参构造器
	 * @throws Exception
	 */
	private Base64DesEncrypt(String desKey) throws Exception {
		DESKeySpec keySpec = new DESKeySpec(desKey.getBytes("UTF-8"));
		iv = new IvParameterSpec(DES_IV);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		key = keyFactory.generateSecret(keySpec);
		cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
	}

	/**
	 * 加密方法
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public synchronized byte[] encode(byte[] data) throws Exception {
		cipher.init(Cipher.ENCRYPT_MODE, key, iv);
		byte[] pasByte = cipher.doFinal(data);
		return Base64.encodeBase64(pasByte);
	}
	/**
	 * 解密方法
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public synchronized byte[] decode(byte[] data) throws Exception {
		cipher.init(Cipher.DECRYPT_MODE, key, iv);
		byte[] pasByte = cipher.doFinal(Base64.decodeBase64(data));
		return pasByte;
	}
	/**
	 * 无参单例
	 * @return
	 * @throws Exception
	 */
	public static Base64DesEncrypt getInstance() throws Exception {
		if (inst == null) {
			inst = new Base64DesEncrypt();
		}
		return inst;
	}
	/**
	 * 有参单例模式获取对象
	 * @return
	 * @throws Exception
	 */
	public static Base64DesEncrypt getInstance(String desKey) throws Exception {
		if (inst == null) {
			inst = new Base64DesEncrypt(desKey);
		}
		return inst;
	}
	
	
	//***************************************华丽的分割线************************************//
	/**
	 * 加密
	 * @param src
	 * @param key
	 * @return
	 */
	public static String encryptDES(String src,String key){
		String text = null;
		try{
			text = new String(getInstance(key).encode(src.getBytes("UTF-8")));
		}catch(Exception e){
			e.printStackTrace();
		}
		return text;
	}
	
	/**
	 * 解密
	 * @param src
	 * @param key
	 * @return
	 */
	public static String decryptDES(String src,String key){
		String text = null;
		try{
			byte[] b = Base64DesEncrypt.getInstance(Constant.JSON_KEY).decode(src.getBytes());
			text = new String(b,"UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		return text;
	}

}
